/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flexengine.server;

import com.flexengine.utils.FlexEngineConsole;
import com.flexengine.logger.Logger;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public class ServerConsole extends FlexEngineConsole {

    public ServerConsole() {
        super("server");
        Runtime.getRuntime().traceInstructions(true);
    }

    @Override
    public void execute(String cmd) {
        if( cmd.equalsIgnoreCase("stop") ) {
            FlexEngineServer.getInstance().stop();
        }
        else if( cmd.equalsIgnoreCase("start") ) {
            FlexEngineServer.getInstance().start();
            
        }
        else if( cmd.equalsIgnoreCase("init") ) {
            FlexEngineServer.getInstance().initialize();
        }
        else if( cmd.equalsIgnoreCase("exit") ) {
            FlexEngineServer.getInstance().stop();
            this.stopWaiting();
        }
        else if( cmd.startsWith("set port") ) {
            cmd = cmd.replace("set port", "").trim();
            try {
                FlexEngineServer.getInstance().setServerPort(Integer.valueOf(cmd));
            } catch(NumberFormatException e) {}
        }
        else if( cmd.startsWith("set max") ) {
            cmd = cmd.replace("set max", "").trim();
            try {
                FlexEngineServer.getInstance().setMaxUsers(Long.valueOf(cmd));
            } catch(NumberFormatException e) {}
        }
        else {
            Logger.logWarning("Command ["+cmd+"] not found!");
        }
    }
    
    private static ServerConsole instance;
    public static ServerConsole getInstance() {
        if( instance==null ) {
            instance = new ServerConsole();
        }
        return instance;
    }
    
}
